﻿using GameMath;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class Building : GameObject
    {
        private readonly int SPAWNING_COOLDOWN = 3000; //ms

        private long internalTimer;
        private World parent;

        public Building(GamePointF position, Size size, World parent)
            : base(position, size)
        {
            this.parent = parent;
            LoadGraphics();
        }

        public override void Update(GameTimer timer)
        {
            internalTimer += timer.LastTickTime;
            if (internalTimer >= SPAWNING_COOLDOWN)
            {

                SpawnEntity();
                internalTimer = 0;
            }
        }

        private void SpawnEntity()
        {
            GamePointF unitPosition = new GamePointF(100,100);
            Size unitSize = new Size(16,16);
            Point unitTarget = new Point(200,300);
            GameVector2F unitTargetAsVector = new GameVector2F(unitPosition, (GamePointF)unitTarget);
            GameVector2F unitVector = new GameVector2F(10f, unitTargetAsVector.Direction);
            Unit unit = new Unit(unitPosition, unitSize, unitVector);
            parent.SpawnEntity(unit);

        }

        private void LoadGraphics()
        {
            Graphics g = Graphics.FromImage(Image);

            PointF[] polygon = new PointF[3];
            polygon[0] = new Point(0, 0);
            polygon[1] = new Point(Size.Width, 0);
            polygon[2] = new Point(Size.Width / 2, Size.Height);

            g.FillPolygon(new SolidBrush(Color.Red), polygon, FillMode.Alternate);
        }
    }
}
